package br.com.advance.automacao.dao.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Query;
import javax.persistence.TemporalType;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import br.com.advance.automacao.dao.MTBFDao;
import br.com.advance.automacao.generic.dao.impl.GenericDaoImpl;
import br.com.advance.automacao.model.MTBF;

@Repository("mtbfDao")
public class MTBFDaoImpl extends GenericDaoImpl<MTBF, Serializable> implements MTBFDao {

	private static final Logger logger = Logger.getLogger(MTBFDaoImpl.class);
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MTBF> buscarPorFiltro(Long idCLP, Long idTurno, Date dataTurno) {
		Query query = entityManager.createNamedQuery(MTBF.BUSCAR_MTBF_POR_FILTRO);
		query.setParameter("idCLP", idCLP);
		query.setParameter("idTurno", idTurno);
		query.setParameter("data", dataTurno, TemporalType.DATE);

		return query.getResultList();
	}
	
	@Override
	public void deletarMTBFPorClpDataTurno(Long idTurno, Date data, Long idCLP) {
		Query query = entityManager.createNamedQuery(MTBF.DELETAR_MTBF_POR_CLP_DATA_TURNO);
		query.setParameter("idCLP", idCLP);
		query.setParameter("idTurno", idTurno);
		query.setParameter("data", data, TemporalType.DATE);
		int qtd = query.executeUpdate();
		if (qtd > 0) {
			logger.warn("Foram deletados "+qtd+" registros do CLP ID: "+ idCLP +" ID Turno: " + idTurno + " Data: " + data);
		}
	}
}
